//
//  GoogleMapViewController.m
//  EasyBuy
//
//  Created by apple on 12-10-20.
//  Copyright (c) 2012年 shen donghao. All rights reserved.
//

#import "GoogleMapViewController.h"
#import "Annotation.h"
#import "GoogleMapInfoData.h"

@interface GoogleMapViewController ()


@end

@implementation GoogleMapViewController
@synthesize mapView;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    locmanager = [[CLLocationManager alloc] init]; 
    [locmanager setDelegate:self]; 
    [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];	
    [locmanager startUpdatingLocation];
    
    CGRect rect = CGRectMake(0, 0, 320, 200);
	mapView = [[MKMapView alloc] initWithFrame:rect];
    [mapView setDelegate: self];
	mapView.showsUserLocation=YES;
    [mapView setMapType: MKMapTypeStandard];
    [self.view addSubview:mapView];
    
}

- (void)setBuyLocationInGoogleMap:(NSMutableArray *)locationArray{
    
    for (GoogleMapInfoData *googleMapInfoData in locationArray) {
        
        CLLocationCoordinate2D Coordinate;
        
        Coordinate.latitude = googleMapInfoData.longitude;
        Coordinate.longitude =googleMapInfoData.latitude;
        Annotation *annotation = [[Annotation alloc] initWithLocation:Coordinate];
        [annotation setTitle:googleMapInfoData.title];
        [annotation setSubtitle:googleMapInfoData.subTitle];
        
        
        [mapView addAnnotation:annotation];
        
        CLLocationCoordinate2D theCoordinate;
        CLLocationCoordinate2D theCenter;
        
        theCoordinate.latitude =googleMapInfoData.latitude;
        theCoordinate.longitude=googleMapInfoData.longitude;
        
        
        
        MKCoordinateRegion theRegin;//查找范围的中心点（圆心）
        theCenter.latitude =googleMapInfoData.latitude;
        theCenter.longitude = googleMapInfoData.longitude;
        theRegin.center=theCenter;
        
        MKCoordinateSpan theSpan;//查找范围的半径
        theSpan.latitudeDelta = 0.1;
        theSpan.longitudeDelta = 0.1;
        theRegin.span = theSpan;
        [mapView setRegion:theRegin];
        [mapView regionThatFits:theRegin];
        [annotation release];

    }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
	//method one：using default pin as a PlaceMarker to display on map
	NSLog(@"creatview");
	MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
	//newAnnotation.pinColor = MKPinAnnotationColorGreen;
	newAnnotation.animatesDrop = YES; 
	//canShowCallout: to display the callout view by touch the pin
	newAnnotation.canShowCallout=YES;
	return newAnnotation;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc{
    [mapView release];
    [locmanager release];
    
    [super dealloc];
}
@end
